package org.hongbo.springbootebsapi.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.hongbo.springbootebsapi.entity.AuthUser;
import org.hongbo.springbootebsapi.service.responsibility.IResponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class InitResponAspect {

    @Autowired
    private IResponService responService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 通过面向切面的方式解决初始化的问题，所有service中，以user结尾的方法都被被拦截并执行初始化
     * 该初始化的作用范围仅限于本service范围
     * 所有需要使用用户变量的方法都应该在最后加入user变量
     * @param joinPoint
     * @param user
     */
    @Before("execution(* org.hongbo.springbootebsapi.service..*.*(..))  && args(..,user)")
    public void methodWithUserImplBefore(JoinPoint joinPoint, AuthUser user){
        log.info(" TestImplBefore  User  我是面向切面测试  before "+ " -->" + joinPoint.getTarget().getClass() + ":" + joinPoint.getSignature().getName()+"  -->"+user.getUserId()+"  --> "+user.getUsername());

        responService.initResponsiblity();
    }


    @After("execution(* org.hongbo.springbootebsapi.service..*.*(..))  && args(..,user)")
    public void methodWithUserImplAfter(JoinPoint joinPoint, AuthUser user){

        log.info(" TestImplBefore  User  我是面向切面测试 after "+ " -->" + joinPoint.getTarget().getClass() + ":" + joinPoint.getSignature().getName()+"  -->"+user.getUserId()+"  --> "+user.getUsername());

    }

}
